import requests

with open('./movie.txt', 'w') as w:
    page_limit = 50
    page_start = 0
    while True:
        url = 'https://movie.douban.com/j/search_subjects'
        r = requests.get(url,
                         params={'type': 'movie', 'tag': '热门', 'page_limit': page_limit, 'page_start': page_start},
                         headers={
                             'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'})
        r.encoding = 'utf8'
        if r.status_code == 200:
            movies = r.json()['subjects']
            print('Response {} movies'.format(len(movies)))
            import json

            for m in movies:
                w.write(json.dumps(m) + '\n')
            if len(movies) < page_limit:
                break
            else:
                page_start += len(movies)
        else:
            print('Error occurred')
            break
